using System;
using System.Collections.Generic;
using System.Text;
using Csla.Data;
using Csla;
using Active.Core.DB;

namespace Active.Core
{
    [Serializable()]
    public class Contacts :
      BusinessListBase<Contacts, Contact>
    {
        #region Business Methods
        protected override object AddNewCore()
        {
            Contact item = Contact.NewContact();
            Add(item);
            return item;
        }

        public Contact GetContactById(int id)
        {
            foreach (Contact item in this)
            {
                if (item.Id == id)
                    return item;
            }
            return null;
        }

        public void Remove(int id)
        {
            foreach (Contact item in this)
            {
                if (item.Id == id)
                {
                    Remove(item);
                    break;
                }
            }
        }

        private int _companyId;
        /// <summary>
        /// Gets the company id.
        /// </summary>
        /// <value>The company id.</value>
        public int CompanyId
        {
            get
            {
                return _companyId;
            }
        }

        private CompanyType _companyCategory;
        public CompanyType Category
        {
            get { return _companyCategory; }
        }


        #endregion

        #region Factory Methods

        //internal static Contacts NewContacts()
        //{
        //    return new Contacts();
        //}

        public static Contacts GetCustomerContacts(int companyId)
        {
            return GetContacts(companyId, CompanyType.Customer);
        }

        public static Contacts GetMediaContacts(int companyId)
        {
            return GetContacts(companyId, CompanyType.Media);
        }

        public static Contacts GetAgentContacts(int companyId)
        {
            return GetContacts(companyId, CompanyType.Agent);
        }

        private static Contacts GetContacts(int companyId, CompanyType category)
        {
            Contacts obj = DataPortal.Fetch<Contacts>(new Criteria(companyId, category));
            return obj;
        }

        private Contacts()
        {
        }

        #endregion

        #region Data Access

        [Serializable()]
        private class Criteria
        {
            private CompanyType _companyCategory;
            public CompanyType CompanyCategory
            {
                get { return _companyCategory; }
            }

            private int _companyId;
            public int CompanyId
            {
                get { return _companyId; }
            }

            public Criteria(int companyId, CompanyType category) 
            {
                _companyCategory = category;
                _companyId = companyId;
            }
        }

        private void DataPortal_Fetch(Criteria criteria)
        {
            RaiseListChangedEvents = false;
            NBearLite.SelectSqlSection select =
                DataService.DBService.Select(Tables.Contacts)
                .Where(Tables.Contacts.CompanyId == criteria.CompanyId
                    && Tables.Contacts.CompanyCategory == criteria.CompanyCategory);

            using (SafeDataReader dr = new SafeDataReader(select.ToDataReader()))
            {
                while (dr.Read())
                {
                    this.Add(Contact.GetContact(dr));
                }
            }
            _companyCategory = criteria.CompanyCategory;
            _companyId = criteria.CompanyId;
            RaiseListChangedEvents = true;
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            RaiseListChangedEvents = false;
            foreach (Contact item in DeletedList)
                item.DeleteSelf();
            DeletedList.Clear();

            foreach (Contact item in this)
            {
                if (item.IsNew)
                {
                    item.Insert(_companyId, _companyCategory);
                }
                else if (item.IsDirty)
                {
                    item.Update(_companyId, _companyCategory);
                }
            }
            RaiseListChangedEvents = true;
        }

        #endregion
    }
}

